package com.gpjcjl.ui.fragment;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.lifecycle.ViewModelProvider;

import com.base.lib.JsonUtil;
import com.base.lib.SharedHelper;
import com.base.lib.ToastUtil;
import com.gpjcjl.BaseActivity;
import com.gpjcjl.Config;
import com.gpjcjl.R;
import com.gpjcjl.adapter.ProduceAdapter;
import com.gpjcjl.entity.Produce;
import com.gpjcjl.entity.SettingProfile;
import com.gpjcjl.event.InitEvent;
import com.gpjcjl.ui.SettingViewModel;
import com.yanzhenjie.recyclerview.OnItemClickListener;
import com.yanzhenjie.recyclerview.OnItemMenuClickListener;
import com.yanzhenjie.recyclerview.SwipeMenuBridge;
import com.yanzhenjie.recyclerview.SwipeMenuItem;
import com.yanzhenjie.recyclerview.SwipeRecyclerView;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 气瓶制造单位
 */
public class SettingItem1 extends BaseActivity implements OnItemClickListener, OnItemMenuClickListener {

    @BindView(R.id.listView)
    SwipeRecyclerView listView;
    private ProduceAdapter adapter;

    private SettingViewModel viewModel;

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEvent(InitEvent event) {
        initData();
    }

    @Override
    protected int getContentView() {
        return R.layout.fragment_item1;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        EventBus.getDefault().register(this);
        viewModel = new ViewModelProvider(this).get(SettingViewModel.class);
        listView.setSwipeMenuCreator((leftMenu, rightMenu, position) -> {
            SwipeMenuItem editItem = new SwipeMenuItem(mContext); // 各种文字和图标属性设置。
            editItem.setText("编辑")
                    .setWidth(160)
                    .setHeight(ViewGroup.LayoutParams.MATCH_PARENT)
                    .setTextColorResource(R.color.white)
                    .setBackgroundColorResource(R.color.c_5B5AFF);
            SwipeMenuItem deleteItem = new SwipeMenuItem(mContext); // 各种文字和图标属性设置。
            deleteItem.setText("删除")
                    .setWidth(160)
                    .setHeight(ViewGroup.LayoutParams.MATCH_PARENT)
                    .setTextColorResource(R.color.white)
                    .setBackgroundColorResource(R.color.c_FF2D55);
            rightMenu.addMenuItem(editItem); // 在Item右侧添加一个菜单。
            //            rightMenu.addMenuItem(deleteItem); // 在Item右侧添加一个菜单。
        });

        listView.setOnItemClickListener(this);
        listView.setOnItemMenuClickListener(this);

        initData();
    }

    private void initData() {
        String json = SharedHelper.getInstance().getString(Config.CACHE_PRODUCE, "[]");
        List<Produce> list = JsonUtil.fromJson(json, List.class, Produce.class);
        listView.setAdapter(adapter = new ProduceAdapter(mContext, list));
    }

    @Override
    public void onItemClick(View view, int position) {
        SettingProfile.setProduce(adapter.getItem(position).getProduce());
        adapter.notifyDataSetChanged();
    }

    @Override
    public void onItemClick(SwipeMenuBridge menuBridge, int position) {
        // 任何操作必须先关闭菜单，否则可能出现Item菜单打开状态错乱。
        menuBridge.closeMenu();
        Produce produce = adapter.getItem(position);
        if (0 == menuBridge.getPosition()) {
            showDialog(produce.getProduce(), produce.getProduce_num(), produce.getId());
        }
    }

    @OnClick({R.id.tv_add})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_add:
                showDialog(null, null, null);
                break;
            default:
                break;
        }
    }

    private void showDialog(String produceName, String produceNum, String id) {
        View dialogView = LayoutInflater.from(mContext).inflate(R.layout.dialog_add_produce, null);
        EditText editProduce = dialogView.findViewById(R.id.edit_produce);
        EditText editNum = dialogView.findViewById(R.id.edit_num);
        editProduce.setText(produceName);
        editNum.setText(produceNum);
        AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
        builder.setTitle("编辑制造单位");
        builder.setView(dialogView);
        builder.setNegativeButton("取消", null);
        builder.setPositiveButton("确定", (dialog, which) -> {
            String produce = editProduce.getText().toString();
            String num = editNum.getText().toString();
            if (TextUtils.isEmpty(produce)) {
                ToastUtil.showToast(mContext, "请输入气瓶制造单位");
            } else if (TextUtils.isEmpty(num)) {
                ToastUtil.showToast(mContext, "请输入气瓶许可证号");
            } else {
                editProduce(produce, num, id);
            }
        });
        builder.create().show();
    }

    private void editProduce(String produce, String produceNum, String id) {
        showLoading();
        viewModel.editProduce(produce, produceNum, id).observe(this, result -> {
            dismissLoading();
            if (result.isSuccess()) {
                viewModel.init();
            }
            ToastUtil.showToast(mContext, result.getMsg());
        });
    }
}
